﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using LibraryIDAL;
using LibraryUtility.Web;
using LibraryUtility;


namespace LibraryDALFactory
{
    /// <summary>
    /// ClassName:DataAccess
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// 可以把所有DAL类的创建放在这个DataAccess类里
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="LibraryDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigHelper.GetConfigString("DAL");
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }
        /// <summary>
        /// 创建TabBook数据层接口
        /// </summary>
        public static ITabBook CreateTabBook()
        {

            string ClassNamespace = AssemblyPath + ".TabBook";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (ITabBook)objType;
        }

        /// <summary>
        /// 创建TabBookState数据层接口
        /// </summary>
        public static ITabBookState CreateTabBookState()
        {

            string ClassNamespace = AssemblyPath + ".TabBookState";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (ITabBookState)objType;
        }

        /// <summary>
        /// 创建TabBookType数据层接口
        /// </summary>
        public static ITabBookType CreateTabBookType()
        {

            string ClassNamespace = AssemblyPath + ".TabBookType";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (ITabBookType)objType;
        }

        /// <summary>
        /// 创建TabBorrowBooks数据层接口
        /// </summary>
        public static ITabBorrowBooks CreateTabBorrowBooks()
        {

            string ClassNamespace = AssemblyPath + ".TabBorrowBooks";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (ITabBorrowBooks)objType;
        }

        /// <summary>
        /// 创建TabUser数据层接口
        /// </summary>
        public static ITabUser CreateTabUser()
        {

            string ClassNamespace = AssemblyPath + ".TabUser";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (ITabUser)objType;
        }
    }
}
